package com.example.s202404028;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Controller
@RequestMapping("/res")
public class ResponseController {
    @RequestMapping("/index")
    public String index() {
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("returnData")
    public String returnData() {
        return "该方法返回数据";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml() {
        return "<h1> Hello, HTML</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson")
    public HashMap<String, String> returnJson() {
        HashMap<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        return map;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response) {
        response.setStatus(418);
        return "设置状态码成功";
    }

    @ResponseBody
    @RequestMapping(value = "/setContentType", produces = "application/json;charset=utf-8")
    public String returnJson2() {
        return "{\"ok\":1}";
    }

    @ResponseBody
    @RequestMapping("/setHeader")
    public String setHeader(HttpServletResponse response) {
        response.setHeader("MyHeader", "MyHeaderValue");
        return "设置成功";
    }
}
